package com.workflow.service.impl;

import java.util.Date;
import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.workflow.bean.Listsales;
import com.workflow.bean.Salescard;
import com.workflow.bean.Salesdeptdetail;
import com.workflow.service.SalescardService;

public class SalescardServiceImpl extends AbstractManager implements
		SalescardService {

	public void addSalescard(String cardno, long listid, String salesdetailno) {
		Salescard salescard = new Salescard();
		Cardinfo cardinfo = (Cardinfo)this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Salesdeptdetail salesdeptdetail = (Salesdeptdetail)this.getHibernateTemplate().load(Salesdeptdetail.class, salesdetailno);
		Listsales listsales = (Listsales)this.getHibernateTemplate().load(Listsales.class, listid);
		
		if(cardinfo != null) {
			Datadict datadictByStatus = new Datadict();
			salescard.setCardno(cardinfo.getCardno());
			if(salesdeptdetail != null) {
				salescard.setSalesdeptdetail(salesdeptdetail);
				cardinfo.setBelongto(salesdetailno);
//				置卡状态为锁定状态  75l
				datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
			}
			if(listid != 0 && listsales != null){
				salescard.setListid(listid);
				cardinfo.setBelongto(listid + "");
//				置卡状态为已销售状态  77l
				datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_SALES);
			}
			//判断卡是否是临近过期在三个月以内
			long vt = cardinfo.getValiddate().getTime();
			long nt = new Date().getTime();
			if(nt > vt || vt - nt < 1000*60*60*24*30) {
				throw new RuntimeException("此卡已过有效期或距有效期在三个月之内！");
			}
			
			cardinfo.setDatadictByStatus(datadictByStatus);
			
			this.getHibernateTemplate().saveOrUpdate(salescard);
			this.getHibernateTemplate().update(cardinfo);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Salescard> findAllSalescard() {
		String hql = "from Salescard sc order by sc.listid ";
		return this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Salescard> findAllSalescardWithSalesdetailno(
			String salesdetailno) {
		String hql = "from Salescard sc where sc.salesdetailno = ? order by sc.listid ";
		return this.getHibernateTemplate().find(hql,salesdetailno);
	}

	public Salescard findSalescard(String cardno) {
		return (Salescard) this.getHibernateTemplate().load(Salescard.class, cardno);
	}

	public PagerModel findSalescardForPager() {
		String hql = "from Salescard sc order by sc.listid ";
		return searchPaginated(hql);
	}

	public void removeSalescard(String cardno) {
		Salescard salescard = (Salescard)this.getHibernateTemplate().load(Salescard.class, cardno);
		if(salescard != null ) {
			this.getHibernateTemplate().delete(salescard);
			Cardinfo cardinfo = (Cardinfo)this.getHibernateTemplate().load(Cardinfo.class, cardno);
			Datadict datadictByStatus = new Datadict();
//			置卡状态为库存 状态编号 76l
			datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
			cardinfo.setDatadictByStatus(datadictByStatus);
			cardinfo.setBelongto("");//取消所属状态，临时
			this.getHibernateTemplate().update(cardinfo);
		}

	}

	public void updateSalescard(String cardno, long listid, String salesdetailno) {
		Salescard salescard = new Salescard();
		Cardinfo cardinfo = (Cardinfo)this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Salesdeptdetail salesdeptdetail = (Salesdeptdetail)this.getHibernateTemplate().load(Salesdeptdetail.class, salesdetailno);
		Listsales listsales = (Listsales)this.getHibernateTemplate().load(Listsales.class, listid);
		
		if(cardinfo != null) {
			salescard.setCardno(cardinfo.getCardno());
			if(salesdeptdetail != null) {
				salescard.setSalesdeptdetail(salesdeptdetail);
				cardinfo.setBelongto(salesdetailno);
			}
			if(listsales != null){
				salescard.setListid(listid);
				cardinfo.setBelongto(listid + "");
			}
			
			Datadict datadictByStatus = new Datadict();
			//置卡状态为锁定状态  75l
			datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
			cardinfo.setDatadictByStatus(datadictByStatus);
			
			this.getHibernateTemplate().saveOrUpdate(salescard);
			this.getHibernateTemplate().update(cardinfo);
		}
	}

}
